<?php
/**
 * Project:      CMS Danneo : Content management system
 * File:         base/danneo.template.php
 * @version	 $Id: Danneo CMS v.0.5.4 Release $
 * @package      CMS Danneo basis kernel
 * @copyright    Copyright (C) 2004 - 2010 Danneo Team. All rights reserved.
 * @link         http://danneo.com, http://danneo.ru
 * @license      http://www.gnu.org/licenses/gpl-2.0.html   GNU General Public License, version 2
 */
if (!defined('DNREAD')) {
	exit();
}

class tm
{
    var $manuale = array();
    var $width = '100';
    var $unmanule = array();

    function tm()
    {
    }

    function create($tpl)
    {
        global $setting;
        if (in_array($tpl,$this->manuale)) {
            $contents = $manuale[$tpl];
        } else {
            $contents = @file_get_contents(DNBASE.'temp/'.$setting['site_temp'].'/'.$tpl.'.tpl');
            $mtpl = str_replace('/','_',$tpl);
            $this->manuale[$mtpl] = $contents;
        }
        return (empty($contents)) ? '<strong>'.$tpl.'.tpl</strong> Not found !<br />' : $contents;
    }

    function block($tpl)
    {
        if (in_array($tpl,$this->manuale)) {
            $contents = $manuale[$tpl];
        } else {
            $contents = @file_get_contents(DNBASE."temp/".$tpl.".tpl");
            $mtpl = str_replace('/','_',$tpl);
            $this->manuale[$mtpl] = $contents;
        }
        return (empty($contents)) ? '<strong>'.$tpl.'.tpl</strong> Not found !<br />' : $contents;
    }

    function parse($carray, $contents)
    {
        global $lang,$setting;
        $newkey = $newval = '';
        foreach ($carray as $key => $value) {
            $newkey[$key] = '{'.$key.'}';
            $newval[$key] = $value;
        }
        $newkey['site_url'] = '{site_url}';
        $newval['site_url'] = $setting['site_url'];
        $newkey['site_temp'] = '{site_temp}';
        $newval['site_temp'] = $setting['site_temp'];
        return str_replace($newkey,$newval,$contents);
    }

    function parseprint($carray, $contents)
    {
        global $lang,$setting;
        $newkey = $newval = '';
        foreach ($carray as $key => $value) {
        	$newkey[$key] = '{'.$key.'}';
        	$newval[$key] = $value;
        }
        $newkey['site_url'] = '{site_url}';
        $newval['site_url'] = $setting['site_url'];
        $newkey['site_temp'] = '{site_temp}';
        $newval['site_temp'] = $setting['site_temp'];
        echo str_replace($newkey,$newval,$contents);
    }

    function contentprint($contents)
    {
        echo $contents;
    }

    function parsein($content)
    {
        if ($count = preg_match_all('#<\!--(if|buffer|add):([a-zA-Z0-9_]*):([a-zA-Z0-9_]*)-->(.*?)<\!--(if|buffer|add)-->#is', $content, $attribut)) {
            for ($i = 0; $i < $count; $i++) {
                 $tags = $attribut[1][$i];
                 $pers = $attribut[2][$i];
                 $vals = $attribut[3][$i];
                 if ($tags == 'if') {
                     if (isset($this->unmanule[$pers])) {
                         if ($this->unmanule[$pers] == $vals) {
                             $content = str_replace('<!--'.$tags.':'.$pers.':'.$vals.'-->','',$content);
                        } else {
                             $content = str_replace('<!--'.$tags.':'.$pers.':'.$vals.'-->'.$attribut[4][$i].'<!--'.$tags.'-->','',$content);
                        }
                     }
                 }
                 if ($tags == 'buffer') {
                     $this->manuale[$pers] = $attribut[4][$i];
                     $content = str_replace('<!--'.$tags.':'.$pers.':'.$vals.'-->'.$attribut[4][$i].'<!--'.$tags.'-->','',$content);
                 }
                 if ($tags == 'add') {
                     if (isset($attribut[4][$i]) && !empty($attribut[4][$i])) {
                         $add = @file_get_contents(DNBASE.$attribut[4][$i]);
                         $content = str_replace('<!--'.$tags.':'.$pers.':'.$vals.'-->'.$attribut[4][$i].'<!--'.$tags.'-->',$add,$content);
                     }
                 }
            }
        }
        return str_replace('<!--if-->','',$content);
    }

    function tableprint($variable,$re = false)
    {
        $r = '';
        $table = array();
        $table[1] = "<table width=\"100%\" class=\"tables\" cellpadding=\"0\" cellspacing=\"0\">\n";
        $table[2] = "                <tr>\n";
        $table[3] = "                  <td style=\"width:".$this->width."%\" class=\"tabletd vt\">\n";
        $table[4] = "                  </td>\n";
        $table[5] = "                </tr>\n";
        $table[6] = "              </table>\n";
        if(is_array($variable)){
            foreach ($variable as $key) {
                if ($re) {
                	$r.= $table[$key];
                } else {
                	echo $table[$key];
                }
            }
        } else {
        	if ($re) {
                $r.= $table[$variable];
            } else {
                echo $table[$variable];
            }
        }
        if ($re) {
            return $r;
        }
    }

    function redirectprint($title, $sec, $message, $url)
    {
        global $setting,$lang;
        header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
        header("Content-Type: text/html; charset=".$setting['langcharset']."");
        $redirlink = '<a href="'.$setting['site_url'].'/'.$url.'">'.$lang['redirect_text'].'</a>';
        $this->parseprint(array('site_url'    => $setting['site_url'],
                                'langcharset' => $setting['langcharset'],
                                'site_temp'   => $setting['site_temp'],
                                'title'       => $lang['redirect_title'],
                                'message'     => $message,
                                'link'        => $redirlink,
                                'url'         => $url,
                                'sec'         => $sec
                                ),$this->create('redirect')
        );
        exit();
    }

    function noaccessprint($re = false)
    {
        global $setting,$userapi,$lang,$subs;
        $userapi->data['linkreg'] = (isset($userapi->data['linkreg']) && (empty($userapi->data['linkreg']) == false)) ? $userapi->data['linkreg'] : 'index.php?dn=user&amp;re=register';
        $userapi->data['linklost'] = (isset($userapi->data['linklost']) && (empty($userapi->data['linklost']) == false)) ? $userapi->data['linklost'] : 'index.php?dn=user&amp;re=login&amp;to=lost';
        $this->unmanule['captcha'] = ($setting['captcha']=='yes' && defined('REMOTE_ADDRS')) ? 'yes' : 'no';
        $form = ($re) ? $this->parsein($this->create('userblock')) : $this->parsein($this->create('noaccess'));
        if ($re) {
            return $this->parse(
            array(
            'ou_title'=>$subs.$lang['ou_title'],
            'message'=>$lang['ou_text'].' <a href="'.$setting['site_url'].'">'.$setting['site'].'</a>.',
            'registr'=>$lang['registr'],
            'login'=>$lang['login'],
            'pass'=>$lang['pass'],
            'linklost'=>$userapi->data['linklost'],
            'linkreg'=>$userapi->data['linkreg'],
            'maxname'=>$setting['maxname'],
            'maxpass'=>$setting['maxpass'],
            'send_pass'=>$lang['send_pass'],
            'enter'=>$lang['enter'],
            'redirect'=>(defined('REDIRECT') ? '<input name="redirect" value="'.REDIRECT.'" type="hidden" />'.(defined('REDIRECTCPU') ? '<input name="redirectcpu" value="1" type="hidden" />' : '') : '')
            ),
            $form);
        } else {
            $this->parseprint(
            array(
            'ou_title'=>$subs.$lang['ou_title'],
            'message'=>$lang['ou_text'].' <a href="'.$setting['site_url'].'">'.$setting['site'].'</a>.',
            'registr'=>$lang['registr'],
            'login'=>$lang['login'],
            'pass'=>$lang['pass'],
            'linklost'=>$userapi->data['linklost'],
            'linkreg'=>$userapi->data['linkreg'],
            'maxname'=>$setting['maxname'],
            'maxpass'=>$setting['maxpass'],
            'send_pass'=>$lang['send_pass'],
            'enter'=>$lang['enter'],
            'redirect'=>(defined('REDIRECT') ? '<input name="redirect" value="'.REDIRECT.'" type="hidden" />'.(defined('REDIRECTCPU') ? '<input name="redirectcpu" value="1" type="hidden" />' : '') : '')
            ),
            $form);

        }
    }

    function norightprint()
    {
        global $lang;
        $this->parseprint(
        array(
        'title'=>$lang['isset_error'],
        'text'=>$lang['no_rights']
        ),
        $this->create('noright'));
    }

    function commentform($mod, $id, $title, $editor, $smilie, $smiliearray, $controlarray, $last)
    {
        global $setting,$lang,$db,$basepref,$usermain;
        $this->parseprint(array('title'=>$lang['comment_add']),$this->manuale['sitetitle']);
        $smilies = '';
        if ($smilie == 'yes' && is_array($smiliearray)) {
            $i = 0;
            $smilies.= '$.smilie = new Array('.sizeof($smiliearray).');';
            while (list(,$img) = @each($smiliearray)) {
                $smilies.=' $.smilie['.$i.'] = new Array(\''.$setting['site_url'].'/'.$img['img'].'\',\''.$img['code'].'\',\''.$img['alt'].'\');';
                $i++;
            }
        }
        $control = $cid = '';
        if ($setting['control'] == 'yes' && is_array($controlarray)) {
            $r = rand(0,sizeof($controlarray)-1);
            $control = @$controlarray[$r]['issue'];
            $cid = @$controlarray[$r]['cid'];
        }
        $comname = empty($setting['userbase']) ? $setting['maxname'] : 0;
        $button = (($editor == 'yes') ? '$.template = \''.$setting['site_temp'].'\';' : '');
        $this->unmanule['captcha'] = ($setting['captcha'] == 'yes' && defined("REMOTE_ADDRS")) ? 'yes' : 'no';
        $this->unmanule['ajax'] = ($setting['ajax'] == 'yes') ? 'yes' : 'no';
        $this->unmanule['control'] = ($setting['control'] == 'yes') ? 'yes' : 'no';
        $form = $this->parsein($this->create('mod/'.$mod.'/form.comment'));
        $activejs = '{smilie:'.(($smilie == 'yes') ? 'true' : 'false').',';
        $activejs.= 'editor:'.(($editor == 'yes') ? 'true' : 'false').'}';
        $this->parseprint(
        array(
        'uname'=>$usermain['uname'],
        'title'=>$title,
        'comment_name'=>$lang['comment_name'],
        'comment_title'=>$lang['all_title'],
        'comment_editor'=>$lang['comment_editor'],
        'all_text'=>$lang['all_text'],
        'all_refresh'=>$lang['all_refresh'],
        'all_sends'=>$lang['all_sends'],
        'comment_list_smilie'=>$lang['comment_list_smilie'],
        'control_word'=>$lang['control_word'],
        'smiliearray'=>$smilies,
        'activejs'=>$activejs,
        'template'=>$button,
        'control'=>$control,
        'comsize'=>$setting['comsize'],
        'comname'=>$comname,
        'last'=>$last,
        'id'=>$id,
        'cid'=>$cid,
        //'val'=>$val,
        'comment_add_button'=>$lang['comment_add_button']
        ),
        $form);
    }

    function noexistprint()
    {
        global $setting,$lang,$api;
        @header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
        //header('HTTP/1.1 404 Not Found');
        //header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
        //header('Content-Type: text/html; charset='.$setting['langcharset'].'');
        $this->parseprint(
        array(
        'site_url'=>$setting['site_url'],
        'langcharset'=>$setting['langcharset'],
        'site_temp'=>$setting['site_temp'],
        'noexit_page_title'=>$lang['noexit_page_title'],
        'message'=>$lang['noexit_page'].' <a href="'.$setting['site_url'].'">'.$setting['site'].'</a>.',
        'go_back'=>$lang['all_goback']
        ),
        $this->create('noexist'));
        exit();
    }

    function closeprint()
    {
        global $setting,$lang,$api;
        header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
        header('Content-Type: text/html; charset='.$setting['langcharset'].'');
        $this->parseprint(array('site_url'    => $setting['site_url'],
                                'langcharset' => $setting['langcharset'],
                                'site_temp'   => $setting['site_temp'],
                                'closedtext'  => $api->siteuni($setting['closedtext'])
                                ),$this->create('closed')
                          );
        exit();
    }
}
$tm = new tm();
?>